package com.jzh.cmhSystem.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jzh.cmhSystem.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserMapper extends BaseMapper<User> {
    @Select({
            "<script>",
            "select *",
            "from t_user",
            "<where>",
            "username = #{username}",
            "</where>",
            "</script>"
    })
    User findUserByName(String username);
    @Select({
            "<script>",
            "select *",
            "from t_user",
            "<where>",
            "id = #{userId}",
            "</where>",
            "</script>"
    })
    User findUserById(Integer userId);
    @Select({
            "<script>",
            "select *",
            "from t_user",
            "<where>",
            "<if test='user.username != null'>",
            "and username like CONCAT('%', #{user.username}, '%')",
            "</if>",
            "</where>",
            "</script>"
    })
    List<User> selectAllUser(@Param("user") User user);
//    @Select({
//            "<script>",
//            "select us.* , co.name AS collegeName, co.leader AS leader,",
//            "from t_user us",
//            "JOIN t_college co ON co.id = us.college_id",
//            "<where>",
//            "us.role = #{role}",
//            "<if test='name != null and name.trim() != \"\"'>",
//            "us.username like CONCAT('%', #{name}, '%')",
//            "</if>",
//            "</where>",
//            "</script>"
//    })
//    List<User> getUserDetailListByRoleAndName(String name, String role);
@Select({
        "<script>",
        "SELECT us.*, co.name AS collegeName, co.leader AS leader",
        "FROM t_user us",
        "JOIN t_college co ON co.id = us.college_id",
        "<where>",
        "us.role = #{role}",
        "<if test='name != null and name.trim() != \"\"'>",
        "AND us.name LIKE CONCAT('%', #{name}, '%')",
        "</if>",
        "</where>",
        "</script>"
})
/*SELECT us.* , co.name AS collegeName, co.leader AS leader from t_user us JOIN t_college co ON co.id = us.college_id  WHERE us.role = 'ATHLETE' AND us.name like concat('%',"张",'%');*/
List<User> getUserDetailListByRoleAndName(@Param("name") String name, @Param("role") String role);
    @Update("UPDATE t_user SET password = #{password} WHERE id = #{id}")
    void updatePwdById(User tuser);
}
